<html>
<html>
<meta charset="utf-8">
<title>首页</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
<style>
    .container {
        margin: 100px auto;
        width: 400px;
        height: 300px;
        background-color: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }
    .LoginBox{
        padding: 40px 15px 15px 15px;
    }
</style>
</head>

<body>
<div class="container">
    <div class="LoginBox">
        <h3>用户登录</h3>
        <form class="ui form">
            <div class="field">
                <label>用户名</label>
                <input type="text" name="username" placeholder="用户名" class="username">
            </div>
            <div class="field">
                <label>密码</label>
                <input type="password" name="password" placeholder="密码" class="password">
            </div>
            <button class="ui teal button"  onclick="return LoginAjax()">登录</button>
        </form>
    </div>
</div>
<script>
    function LoginAjax(){
        var data = {};
        data['username'] = $('.username').val();
        data['password'] = $('.password').val();
        if(data.username == '' || data.password == ''){
            alert("表单数据不能为空")
            return false;
        }else{
            $.ajaxSetup({contentType: "application/json; charset=utf-8"})
            $.post("login", JSON.stringify(data), function (res) {
                console.log(res)
                if (res.flag){
                    location.href = 'chatroom';
                    return true
                }else {
                    alert(JSON.stringify(res));
                    return false
                }
            }, 'json');
        }
        return false
    }
</script>
</body>

</html>